﻿import Vue from '/vue/libs/vue.esm.browser.min.js'
import Router from '/vue/libs/vue-router.esm.browser.js'
import { isDemo } from '/vue/utils/validate.js'
import modulesFilesDemo from '/nodejs/require?dir=vue/router/modules/demo&sub=true&ext=js';
import modulesFilesFormal from '/nodejs/require?dir=vue/router/modules/formal&sub=true&ext=js';

const modulesDemo = modulesFilesDemo.keys().reduce((modules, modulePath) => {
	const json = modulesFilesDemo(modulePath);
	const router = eval("(" + json + ")");
	modules.push(router)
	return modules
}, [])

const modulesFormal = modulesFilesFormal.keys().reduce((modules, modulePath) => {
	const json = modulesFilesFormal(modulePath);
	const router = eval("("+json+")");
	modules.push(router)
	return modules
}, [])

Vue.use(Router)
const l = httpVueLoader;
let constantRoutes = [], asyncRoutes = [];

if (isDemo()) {
	constantRoutes = [
		{
			path: '/redirect',
			component: Layout,
			hidden: true,
			children: [
				{
					path: '/redirect/:path*',
					component: l('/vue/views/redirect/index.vue')
				}
			]
		},
		{
			path: '/login',
			component: login,
			hidden: true
		},
		{
			path: '/404',
			component: l('/vue/views/error-page/404.vue'),
			hidden: true
		},
		{
			path: '/401',
			component: l('/vue/views/error-page/401.vue'),
			hidden: true
		},
		{
			path: '/',
			component: Layout,
			redirect: '/example',
			children: [
				{
					path: 'dashboard',
					component: l('/vue/views/dashboard/index.vue'),
					name: 'Dashboard',
					meta: { title: '系统主页', icon: 'dashboard', affix: true }
				}
			]
		},
		{
			path: '/documentation',
			component: Layout,
			children: [
				{
					path: 'index',
					component: l('/vue/views/documentation/index.vue'),
					name: 'Documentation',
					meta: { title: 'Documentation', icon: 'documentation', affix: true }
				}
			]
		},
		{
			path: '/guide',
			component: Layout,
			redirect: '/guide/index',
			children: [
				{
					path: 'index',
					component: l('/vue/views/guide/index.vue'),
					name: 'Guide',
					meta: { title: 'Guide', icon: 'guide', noCache: true }
				}
			]
		},
		{
			path: '/profile',
			component: Layout,
			redirect: '/profile/index',
			hidden: true,
			children: [
				{
					path: 'index',
					component: l('/vue/views/profile/index.vue'),
					name: 'Profile',
					meta: { title: 'Profile', icon: 'user', noCache: true }
				}
			]
		}
	];
	asyncRoutes = [
		{
			path: '/permission',
			component: Layout,
			hidden: false,
			redirect: '/permission/page',
			alwaysShow: true, // will always show the root menu
			name: 'Permission',
			meta: {
				title: 'Permission',
				icon: 'lock',
				roles: ['admin', 'editor'] // you can set roles in root nav
			},
			children: [
				{
					path: 'page',
					component: l('/vue/views/permission/page.vue'),
					name: 'PagePermission',
					meta: {
						title: 'Page Permission',
						roles: ['admin'] // or you can only set roles in sub nav
					}
				},
				{
					path: 'directive',
					component: l('/vue/views/permission/directive.vue'),
					name: 'DirectivePermission',
					meta: {
						title: 'Directive Permission'
						// if do not set roles, means: this page does not require permission
					}
				},
				{
					path: 'role',
					component: l('/vue/views/permission/role.vue'),
					name: 'RolePermission',
					meta: {
						title: 'Role Permission',
						roles: ['admin']
					}
				}
			]
		},

		{
			path: '/icon',
			component: Layout,			
			children: [
				{
					path: 'index',
					component: l('/vue/views/icons/index.vue'),
					name: 'Icons',					
					meta: { title: 'Icons', icon: 'icon', noCache: true }
				}
			]
		},

		...modulesDemo,

		{
			path: '/example',
			component: Layout,
			redirect: '/example/list',
			name: 'Example',
			meta: {
				title: 'Example',
				icon: 'el-icon-apple'
			},
			children: [
				{
					path: 'create',
					component: l('/vue/views/example/create.vue'),
					name: 'CreateArticle',
					meta: { title: 'Create Article', icon: 'edit' }
				},
				{
					path: 'edit/:id(\\d+)',
					component: l('/vue/views/example/edit.vue'),
					name: 'EditArticle',
					meta: { title: 'Edit Article', noCache: true, activeMenu: '/example/list' },
					hidden: true
				},
				{
					path: 'list',
					component: l('/vue/views/example/list.vue'),
					name: 'ArticleList',
					meta: { title: 'Article List', icon: 'el-icon-apple' }
				},
				{
					path: '多级演示',
					component: l('/vue/components/menu/二级目录.vue'),
					name: '多级演示',
					alwaysShow:true,
					meta: { title: '多级演示', icon: 'el-icon-apple' },
					children: [		
						{
							path: 'create',
							component: l('/vue/views/example/create.vue'),
							name: 'CreateArticle2',
							meta: { title: 'Create Article', icon: 'edit' }
						},
						{
							path: 'list',
							component: l('/vue/views/example/list.vue'),
							name: 'ArticleList2',
							meta: { title: 'Article List', icon: 'el-icon-apple' }
						},
						{
							path: '上下文菜单演示',
							component: l('/vue/views/example/上下文菜单示例.vue'),
							name: '上下文菜单演示',
							meta: { title: '上下文菜单演示', icon: 'el-icon-apple' }
						}
					]
				}
			]
		},

		{
			path: '/tab',
			component: Layout,
			children: [
				{
					path: 'index',
					component: l('/vue/views/tab/index.vue'),
					name: 'Tab',
					meta: { title: 'Tab', icon: 'tab' }
				}
			]
		},

		{
			path: '/error',
			component: Layout,
			redirect: 'noRedirect',
			name: 'ErrorPages',
			meta: {
				title: 'Error Pages',
				icon: '404'
			},
			children: [
				{
					path: '401',
					component: l('/vue/views/error-page/401.vue'),
					name: 'Page401',
					meta: { title: '401', noCache: true }
				},
				{
					path: '404',
					component: l('/vue/views/error-page/404.vue'),
					name: 'Page404',
					meta: { title: '404', noCache: true }
				}
			]
		},

		{
			path: '/error-log',
			component: Layout,
			children: [
				{
					path: 'log',
					component: l('/vue/views/error-log/index.vue'),
					name: 'ErrorLog',
					meta: { title: 'Error Log', icon: 'bug' }
				}
			]
		},

		{
			path: '/excel',
			component: Layout,
			redirect: '/excel/export-excel',
			name: 'Excel',
			meta: {
				title: 'Excel',
				icon: 'excel'
			},
			children: [
				{
					path: 'export-excel',
					component: l('/vue/views/excel/export-excel.vue'),
					name: 'ExportExcel',
					meta: { title: 'Export Excel' }
				},
				{
					path: 'export-selected-excel',
					component: l('/vue/views/excel/select-excel.vue'),
					name: 'SelectExcel',
					meta: { title: 'Export Selected' }
				},
				{
					path: 'export-merge-header',
					component: l('/vue/views/excel/merge-header.vue'),
					name: 'MergeHeader',
					meta: { title: 'Merge Header' }
				},
				{
					path: 'upload-excel',
					component: l('/vue/views/excel/upload-excel.vue'),
					name: 'UploadExcel',
					meta: { title: 'Upload Excel' }
				}
			]
		},

		{
			path: '/zip',
			component: Layout,
			redirect: '/zip/download',
			alwaysShow: true,
			name: 'Zip',
			meta: { title: 'Zip', icon: 'zip' },
			children: [
				{
					path: 'download',
					component: l('/vue/views/zip/index.vue'),
					name: 'ExportZip',
					meta: { title: 'Export Zip' }
				}
			]
		},

		{
			path: '/pdf',
			component: Layout,
			redirect: '/pdf/index',
			children: [
				{
					path: 'index',
					component: l('/vue/views/pdf/index.vue'),
					name: 'PDF',
					meta: { title: 'PDF', icon: 'pdf' }
				}
			]
		},
		{
			path: '/pdf/download',
			component: l('/vue/views/pdf/download.vue'),
			hidden: true
		},

		{
			path: '/theme',
			component: Layout,
			children: [
				{
					path: 'index',
					component: l('/vue/views/theme/index.vue'),
					name: 'Theme',
					meta: { title: 'Theme', icon: 'theme' }
				}
			]
		},

		{
			path: '/clipboard',
			component: Layout,
			children: [
				{
					path: 'index',
					component: l('/vue/views/clipboard/index.vue'),
					name: 'ClipboardDemo',
					meta: { title: 'Clipboard', icon: 'clipboard' }
				}
			]
		},

		{
			path: '/external-link',
			component: Layout,
			children: [
				{
					path: 'https://element.eleme.cn/#/zh-CN/component/cascader',
					meta: { title: '外链:element', icon: 'link' }
				}
			]
		},
		{
			path: '/iframe例子',
			component: Layout,
			children: [
				{
					path: '渲染函数&JSX',
					name: '渲染函数&JSX',
					component: l("/vue/components/menu/iframe.vue"),
					meta: {
						title: 'iframe:渲染函数&JSX', icon: 'link',
						iframeSrc: "https://cn.vuejs.org/v2/guide/render-function.html"
					}
				}
			]
		},
		// 404 page must be placed at the end !!!
		{ path: '*', redirect: '/404', hidden: true }
	];
}
else {
	constantRoutes = [
		{
			path: '/redirect',
			component: Layout,
			hidden: true,
			children: [
				{
					path: '/redirect/:path*',
					component: l('/vue/views/redirect/index.vue')
				}
			]
		},
		{
			path: '/login',
			component: login,
			hidden: true
		},		
		{
			path: '/404',
			component: l('/vue/views/error-page/404.vue'),
			hidden: true
		},
		{
			path: '/401',
			component: l('/vue/views/error-page/401.vue'),
			hidden: true
		},	
		
		{
			path: '/profile',
			component: Layout,
			redirect: '/profile/index',
			hidden: true,
			children: [
				{
					path: 'index',
					component: l('/vue/views/profile/index.vue'),
					name: 'Profile',
					meta: { title: 'Profile', icon: 'user', noCache: true }
				}
			]
		},
		{
			path: '/',
			component: Layout
		}
	];

	asyncRoutes = modulesFormal;
}



/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
	roles: ['admin','editor']    control the page roles (you can set multiple roles)
	title: 'title'               the name show in sidebar and breadcrumb (recommend set)
	icon: 'svg-name'             the icon show in the sidebar
	noCache: true                if set true, the page will no be cached(default is false)
	affix: true                  if set true, the tag will affix in the tags-view
	breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
	activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export { constantRoutes };

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export { asyncRoutes };

const createRouter = () => new Router({
	// mode: 'history', // require service support
	scrollBehavior: () => ({ y: 0 }),
	routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
	const newRouter = createRouter()
	router.matcher = newRouter.matcher // reset router
}

export default router
